package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class listController {
    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 商品搜索
     *
     * @param searchParam
     * @return
     */
    @RequestMapping("list.html")
    public String list(SearchParam searchParam, Model model) {
        Result<Map> result = listFeignClient.list(searchParam);
        String trademarkParam = makeTrademarkParam(searchParam.getTrademark());
        List<Map> propsParamList = makeProps(searchParam.getProps());
        model.addAllAttributes(result.getData());
        String urlParam = makeUrlParam(searchParam);
        Map<String, Object> orderMap = dealOrder(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);
        model.addAttribute("propsParamList", propsParamList);
        model.addAttribute("trademarkParam", trademarkParam);
        model.addAttribute("searchParam", searchParam);
        model.addAttribute("urlParam", urlParam);
        return "list/index";
    }

    private Map<String,Object> dealOrder(String order) {
        HashMap<String, Object> orderMap = new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split!=null&&split.length==2){
                orderMap.put("type",split[0]);
                orderMap.put("sort",split[1]);
            }
        }else {
            orderMap.put("type","1");
            orderMap.put("sort","asc");
        }
        return orderMap;
    }

    private List<Map> makeProps(String[] props) {
        List<Map> list = new ArrayList<>();
        if (props != null && props.length > 0) {
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("attrId", split[0]);
                    hashMap.put("attrValue", split[1]);
                    hashMap.put("attrName", split[2]);
                    list.add(hashMap);
                }
            }
        }
        return list;
    }

    private String makeTrademarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            if (split.length > 0 && split != null)
                return "品牌:" + split[1];
        }
        return null;
    }

    /**
     * 制作请求url
     *
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
//        StringBuilder urlParam = new StringBuilder();
//        if (null != searchParam.getKeyword()) {
//            urlParam.append("keyWord=").append(searchParam.getKeyword());
//        }
//        if (searchParam.getCategory1Id() != null) {
//            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
//        }
//        if (searchParam.getCategory2Id() != null) {
//            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
//        }
//        if (searchParam.getCategory3Id() != null) {
//            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
//        }
//        if (searchParam.getTrademark() != null) {
//            if (urlParam.length() > 0)
//                urlParam.append("&trademark=").append(searchParam.getTrademark());
//        }
//        if (null != searchParam.getProps()) {
//            for (String prop : searchParam.getProps()) {
//                if (urlParam.length() > 0)
//
//                    urlParam.append("&props=").append(prop);
//            }
//        }
//        return "list.html? " + urlParam.toString();
        //  声明一个对象
        StringBuilder sb = new StringBuilder();

        //  判断用户是否通过关键检索：
        //  http://list.gmall.com/list.html?keyword=手机
        if(!StringUtils.isEmpty(searchParam.getKeyword())){
            sb.append("keyword=").append(searchParam.getKeyword());
        }
        //  http://list.gmall.com/list.html?category3Id=61
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            sb.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //  http://list.gmall.com/list.html?category2Id=13
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            sb.append("category2Id=").append(searchParam.getCategory2Id());
        }
        //  http://list.gmall.com/list.html?category1Id=2
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            sb.append("category1Id=").append(searchParam.getCategory1Id());
        }

        //  判断用户是否通过品牌检索
        //  http://list.gmall.com/list.html?category3Id=61&trademark=4:小米
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            //  进行拼接
            if (sb.length()>0){
                sb.append("&trademark=").append(trademark);
            }
        }
        //  判断用户是否通过平台属性进行检索：
        //  http://list.gmall.com/list.html?category3Id=61&trademark=4:小米&props=2342:4.7英寸:屏幕&props=2345:1:1
        String[] props = searchParam.getProps();
        if (props!=null && props.length>0){
            //  循环遍历
            for (String prop : props) {
                if (sb.length()>0){
                    sb.append("&props=").append(prop);
                }
            }
        }

        //  sb list.html 后面的参数列表
        return "list.html?"+sb.toString();
    }
}
